//package test;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import DBConnectAndRetrieve;
import org.jdom.output.XMLOutputter;
import org.jdom.Document;

//import org.json.simple.JSONObject;
public class TravelAppServer implements Runnable {

    double LATITUDE = 37.42233;
    double LONGITUDE = -122.083;
    // Note: The default XPath expression "/" selects
    // all text within the XML.
    private static String XPATH_EXPRESSION = "//text()";
    Document xmlString;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Thread(new TravelAppServer()).start();
    }

    public void run() {
        // TODO Auto-generated method stub
        try {
            System.out.println("S: Connecting...");
            ServerSocket serverSocket = new ServerSocket(8221);
            while (true) {
                Socket client = serverSocket.accept();
                System.out.println("S: Receiving...");

                try {
                    PrintWriter out = new PrintWriter(
                            client.getOutputStream(), true);
                    BufferedReader in = new BufferedReader(new InputStreamReader(
                            client.getInputStream()));
                    String inputLine, outputLine;


                    while ((inputLine = in.readLine()) != null) {
                        System.out.println("S: Received: '" + inputLine + "'");
                        xmlString = getLocationInfo(inputLine);
                        //xmlString = getLocationInfo("41.034272,-77.516815,museum,4");
                        try {
                            XMLOutputter outputter = new XMLOutputter();
                            outputter.output(xmlString, System.out);
                            String retStr = outputter.outputString(xmlString);
                            out.println(retStr);
                        } catch (java.io.IOException e) {
                            e.printStackTrace();
                        }
                        //System.out.println("S: Sending: '" + xmlString.getDescendants().toString() + "'");
                        //outputLine = xmlString;
                    }

                    /*BufferedReader in = new BufferedReader(
                    new InputStreamReader(client.getInputStream()));

                    String str = in.readLine();

                    System.out.println("S: Received: '" + str + "'");
                    xmlString = getLocationInfo(str);

                    PrintWriter out=new PrintWriter(client.getOutputStream(),true);

                    out.println(xmlString);*/


                } catch (Exception e) {
                    System.err.println("S: Error"+e);
                    e.printStackTrace();

                } finally {
                    client.close();
                    System.out.println("S: Done.");
                }
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("S: Error");
            e.printStackTrace();
        }
    }

    public static Document getLocationInfo(String inputString) {
        String[] param = inputString.split(",");
        DBConnectAndRetrieve app = new DBConnectAndRetrieve();
        Document resDoc = app.getTable(param[0],param[1],param[2],param[3]);
        //for debug purpose
        return resDoc;
    }
}
